/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import com.sun.lwuit.Command;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import core.DriverManager;
import core.ScreenEngine;
import network.Connection;
import network.PacketBuilder;
import view.additional.CellItem;
import view.additional.ThumbList;
import view.additional.TickerRenderer;
import view.commands.BackCommand;

/**
 *
 * @author Kvest
 */
public class TimeFunctionForm extends Form 
{
    private static final int SELECT_COMMAND_ID = 1;
     
    private ThumbList items;
    private Command select;
    
    private int orderID;
    
    public TimeFunctionForm()
    {
        super("functions");
        
        orderID = 0;
        
        init();
    }
    
    private void init()
    {
        setLayout(new BorderLayout());
        setScrollable(false);
        
        //Создаем список
        items = new ThumbList();
        items.setSelectCommandText("...");
        items.setFixedSelection(ThumbList.FIXED_NONE_CYCLIC);
        TickerRenderer render = new TickerRenderer();
        items.setRenderer(render);
        
        initItems();
        addComponent(BorderLayout.CENTER, items);
        
        //Команды
        select = new Command("select",SELECT_COMMAND_ID)
        {
            public void actionPerformed(ActionEvent evt)
            {
                selectAction();
                
                evt.consume();
            }
        };
        addCommand(select);
        
        items.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent evt) 
            {
                select.actionPerformed(evt);
            }
        });
        
        addCommand(new BackCommand());
    }
    
    private void initItems()
    {
        String min = (new Label("min")).getText();
        items.addItem(new CellItem(5, "5 " + min));
        items.addItem(new CellItem(10, "10 " + min));
        items.addItem(new CellItem(15, "15 " + min));
        items.addItem(new CellItem(20, "20 " + min));
        items.addItem(new CellItem(30, "30 " + min));
        items.addItem(new CellItem(45, "45 " + min));
        items.addItem(new CellItem(60, "60 " + min));        
    }
    
    public void setOrderID(int orderID)
    {
        this.orderID = orderID;
    }
    
    private void selectAction()
    {
        CellItem selected = (CellItem)items.getSelectedItem();
        if (selected != null)
        {
            //Отображаем форму прогресса
            ScreenEngine.getEngine().push(new ProgressForm());
            
            //Формируем запрос
            byte[] data = PacketBuilder.createGetOrderRequest(DriverManager.getManager().getDriverID(), orderID, selected.getId());
            
            //Отправляем запрос
            boolean sendSuccess = Connection.getConnection().send(data);
            
            //Удаляем формы ожидания, выбора времени и описания заказа
            ScreenEngine.getEngine().popTo(ScreenEngine.getEngine().getScreensCount() - 4);
            
            //Отображаем сообщение
           // MessageBox.showModeless(sendSuccess ? "message" : "error", sendSuccess ? "request_sended" : "request_error");
        }
    }
}
